\documentclass{article}

\begin{document}

<<>>=
sink("main_log_morocco.txt", append=F, type="output")
@

<<>>=
library(foreign)
library(cregg)

cjmaroc = read.csv("Morocco.csv")
cjmaroc <- subset(cjmaroc, cjmaroc$MOROCCONat != "Expat")

cjmaroc$Elections <- factor(cjmaroc$Elections)
cjmaroc$Elections <- relevel(cjmaroc$Elections, ref = "There are elections with multiple recognized political parties")
cjmaroc$Participation <- factor(cjmaroc$Participation)
cjmaroc$Official_Religion <- factor(cjmaroc$Official_Religion)
cjmaroc$Role_Religion <- factor(cjmaroc$Role_Religion)
cjmaroc$Services <- factor(cjmaroc$Services)
cjmaroc$Unemployment_Rate <- factor(cjmaroc$Unemployment_Rate)
@

<<Figure 3 & Appendix D>>=
f1 <- Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate

amces <- cj(cjmaroc, f1, id = ~RecordNo)
pdf(file="figure3_morocco.pdf")
plot(amces, legend_pos = "none")
dev.off()

pdf(file="appendixd_morocco.pdf")
plot(mm(cjmaroc, f1, id = ~RecordNo), vline = 0.5, legend_pos = "none")
dev.off()
@

<<Appendix C. College educated>>=
cjmaroc$educ <- 0
cjmaroc$educ <- ifelse(cjmaroc$education=="University first degree (e.g. BA, BSc)", 1, cjmaroc$educ)
cjmaroc$educ <- ifelse(cjmaroc$education=="University higher degree (Masters, MBA, PhD)", 1, cjmaroc$educ)
cjmaroc$educ <- ifelse(cjmaroc$education=="Professional higher education (e.g. to qualify as a lawyer, accountant)", 1, cjmaroc$educ)
cjmaroc$educ <- factor(cjmaroc$educ)

mm_diffs(cjmaroc, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate, ~ educ, id = ~ RecordNo)

pdf(file="appendixc_morocco.pdf")
plot(mm_diffs(cjmaroc, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate, ~ educ, id = ~ RecordNo),  legend_pos = "none")
dev.off()
@

<<Appendix E>>=
table(cjmaroc$Unemployment_Rate)
appendixe <- cjmaroc[ which(cjmaroc$Unemployment_Rate=="High unemployment (14%)"),]
pdf(file="appendixe_high_morocco.pdf")
plot(cj(appendixe, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services , id = ~RecordNo), legend_pos = "none") #high unemployment side
dev.off()
appendixe <- cjmaroc[ which(cjmaroc$Unemployment_Rate=="Low unemployment (7%)"),]
pdf(file="appendixe_low_morocco.pdf")
plot(cj(appendixe, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services , id = ~RecordNo), legend_pos = "none") #low unemployment side
dev.off()
@

<<Appendix F>>=
table(cjmaroc$Services)
appendixf <- cjmaroc[ which(cjmaroc$Services=="Good government provision of basic items (ie housing and food) to individuals"),]
pdf(file="appendixf_good_morocco.pdf")
plot(cj(appendixf, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #good provision side
dev.off()
appendixf <- cjmaroc[ which(cjmaroc$Services=="Some government provision of basic items (ie housing and food) to individuals"),]
pdf(file="appendixf_some_morocco.pdf")
plot(cj(appendixf, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #some provision side
dev.off()
appendixf <- cjmaroc[ which(cjmaroc$Services=="Little government provision of basic items (ie housing and food) to individuals"),]
pdf(file="appendixf_little_morocco.pdf")
plot(cj(appendixf, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #little provision side
dev.off()
@

<<Appendix G>>=
table(cjmaroc$Elections)
appendixg <- cjmaroc[ which(cjmaroc$Elections=="There are elections with multiple recognized political parties"),]
pdf(file="appendixg_multi_morocco.pdf")
plot(cj(appendixg, Choice ~ Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #multi party side
dev.off()
appendixg <- cjmaroc[ which(cjmaroc$Elections=="There are elections with one recognized political party"),]
pdf(file="appendixg_one_morocco.pdf")
plot(cj(appendixg, Choice ~ Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #one party side
dev.off()
appendixg <- cjmaroc[ which(cjmaroc$Elections=="There are no elections"),]
pdf(file="appendixg_none_morocco.pdf")
plot(cj(appendixg, Choice ~ Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #no elections side
dev.off()
@

<<Appendix H>>=
cjmaroc$dembest <- 0
cjmaroc$dembest[cjmaroc$Q8 == "An elected government is always preferable to any other kind of government"] = 1
cjmaroc$dembest <- as.factor(cjmaroc$dembest)

mm_diffs(cjmaroc, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate, ~ dembest, id = ~ RecordNo, legend_pos = "none")

pdf(file="appendixh_morocco.pdf")
plot(mm_diffs(cjmaroc, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate, ~ dembest, id = ~ RecordNo), legend_pos = "none")
dev.off()
@

<<Appendix B>>=
cj_props(cjmaroc, ~ Elections, id = ~ CaseID)
cj_props(cjmaroc, ~ Participation, id = ~ CaseID)
cj_props(cjmaroc, ~ Official_Religion, id = ~ CaseID)
cj_props(cjmaroc, ~ Role_Religion, id = ~ CaseID)
cj_props(cjmaroc, ~ Services, id = ~ CaseID)
cj_props(cjmaroc, ~ Unemployment_Rate, id = ~ CaseID)
@

<<>>=
sink() # final line here
@
\end{document}
